<?php

class CarLibraryAction extends Action {

    /**
     * 车型库首页
     * @param string $parentID
     * @param int $price
     * @param int $stype
     * @param int $pageSize
     * @param int $page
     * @param string $order
     */
    public function index($parentID = "",$price = 99,$stype = 99,$pageSize = 10,$page = 0,$order = "up"){

        $this->CarLibIndex($parentID,$price,$stype,$pageSize,$page,$order);
        $this->display();

    }

    /**
     * 车型库
     */
    public function carlib(){
        $Model = new Model();
        $StrSqlGCRan = "Select top 12  NewID() as random, ClassID,ClassName,Zimu,Logo From Group_Class Where ParentID='0' order by NewID()";
        $DataGcRan = $Model->query( $StrSqlGCRan );

        $StrSqlGCcar = "select top 10 gc.id, gc.ClassID,gc.ClassName,gcr.counter,gcr.jibie,gcr.thumbnail,gcr.mincsprice,gcr.maxcsprice,gcj.minjxsprice,gcj.maxjxsprice From Group_Class gc LEFT JOIN (SELECT ClassID, sum(hits) as counter,  min(csprice) mincsprice,MAX(csprice) maxcsprice ,min(thumbnail) as thumbnail ,min(jibie) as jibie from Group_Car GROUP BY ClassID) gcr  ON gcr.ClassID = gc.ClassID LEFT JOIN (SELECT ClassID,min(jxsPrice) minjxsprice,MAX(jxsPrice) maxjxsprice from Group_Car_Jxs GROUP BY classid ) gcj on gcj.ClassID = gc.ClassID where gcr.thumbnail is not null  ORDER BY  gcr.counter DESC";
        $DataGCcar= $Model->query( $StrSqlGCcar );

        $StrSqlGC = "Select ClassID,ClassName,Zimu From Group_Class Where ParentID='0' Order By zimu Asc,Orders Asc";
        $DataGc = $Model->query( $StrSqlGC );

        $this->assign('DataGc',$DataGc);
        $this->assign('DataGCcar',$DataGCcar);
        $this->assign('DataGcRan',$DataGcRan);
        $this->display();
    }

    /**
     * 详细车型介绍
     * @param string $ClassID
     * @param string $Type
     */
    public function carInfo($ClassID = '0134071320',$CarID = "" , $Stype = "Car"){

        $Model = new Model();

            //车型列表
            $StrSqlCars = "select gc.id,gc.title,gc.classid,gc.classname,gc.pz,gc.csprice,gc.thumbnail from Group_Car gc where gc.classid='".$ClassID."' and not_new=0  Order By gc.nf desc";
            $DataCars= $Model->query( $StrSqlCars );

            if($CarID == ""){
                $DataSelCar = $DataCars[0];
            }else{
                //选择的车型
                $StrSqlCars = "select top 1 gc.id,gc.title,gc.classid,gc.classname,gc.pz,gc.csprice,gc.thumbnail from Group_Car gc where gc.id='".$CarID."' and not_new=0  Order By gc.nf desc";
                $DataSelCar= $Model->query( $StrSqlCars );
                $DataSelCar=$DataSelCar[0];
            }

        if($Stype == "Car"){
            //车型参数
            $StrSqlCarTip = "select Title,zimu,ClassName from group_car_tips order by orders1,orders2";
            $DataCarTip= $Model->query( $StrSqlCarTip );
            $this->assign('DataCarTip',$DataCarTip);
        }else{
            //父类classid
            $StrSqlCarClassID = "SELECT ID,ClassID,ClassName,ThisPath,ParentID FROM Group_Class WHERE ClassID = '".$ClassID."'";
            $DataJXS =  $Model->query( $StrSqlCarClassID );

            //经销商
            $StrSqlJXS = "select GClassID,company,domain,address,saletel,ismember from group_sort where classidt LIKE '%".$DataJXS[0]['ParentID'].",%' and area_name='".C("APP_URL_AREA")."'";
            $DataJXS =  $Model->query( $StrSqlJXS );
            $this->assign('DataJXS',$DataJXS);

        }

        $this->assign('CarID',$CarID);
        $this->assign('DataCars',$DataCars);
        $this->assign('DataSelCar',$DataSelCar);
        $this->assign('Stype',$Stype);

        $this->display();
    }

    /**
     *
     * @param $CarID
     */
    public function carDetail($CarID){
        $Model = new Model();
        $StrSqlCarDetail = "select g.*,c.nf from group_car_detail g left join sxol..group_car c on g.carid=c.id Where g.CarID='".$CarID."'";

        $DataCarDetail= $Model->query( $StrSqlCarDetail );

        if ($DataCarDetail){
            // 成功后返回客户端新增的用户ID，并返回提示信息和操作状态
            $this->ajaxReturn($DataCarDetail,"查询成功！",1);
        }else{
            // 错误后返回错误的操作状态和提示信息
            $this->ajaxReturn(0,"查询错误！",0);
        }


    }

    /**
     * 详细车型介绍
     * @param string $ClassID
     * @param string $Type
     */
    public function car($ClassID = '0134071320',$Type = "zongshu"){
        $this->Carcar($ClassID,$Type);
        $this->display();
    }

    /**
     * 详细车型介绍
     * @param string $ClassID
     * @param string $Type
     */
    public function Carcar($ClassID = '0134071320',$Type = "zongshu"){

        $notNew = 0;
        $Model = new Model();

        //汽车基本信息
        $StrSqlCar = "select gc.id, gc.ClassID,gc.ParentID,gc.ClassName,gcr.pinpai,gcr.pl,gcr.zd,gcr.bsx,gcr.jibie,gcr.thumbnail,gcr.mincsprice,gcr.maxcsprice,gcj.minjxsprice,gcj.maxjxsprice From Group_Class gc LEFT JOIN (SELECT ClassID,min(pinpai) pinpai,max(bsx) bsx,max(pl) pl ,max(zd) as zd,min(csprice) mincsprice,MAX(csprice) maxcsprice ,min(thumbnail) as thumbnail ,min(jibie) as jibie from Group_Car GROUP BY ClassID) gcr  ON gcr.ClassID = gc.ClassID LEFT JOIN (SELECT ClassID,min(jxsPrice) minjxsprice,MAX(jxsPrice) maxjxsprice from Group_Car_Jxs GROUP BY classid ) gcj on gcj.ClassID = gc.ClassID where gcr.thumbnail is not null and gc.ClassID = '".$ClassID."' ORDER BY  gc.id";
        $DataCar = $Model->query( $StrSqlCar );

        switch ( $Type ) {
            case  "chexin" :

                $StrSqlCarNews = "select gc.id,gc.title,gc.classid,gc.pz,gc.csprice,gc.thumbnail from Group_Car gc where gc.classid='".$ClassID."' and not_new = 0  Order By gc.pz desc";
                $DataCarNews= $Model->query( $StrSqlCarNews );

                $StrSqlCarOlds = "select  gc.id,gc.title,gc.classid,gc.pz,gc.csprice,gc.thumbnail from Group_Car gc where gc.classid='".$ClassID."' and not_new = 1  Order By gc.pz desc";
                $DataCarOlds= $Model->query( $StrSqlCarOlds );

                $this->assign('DataCarNews',$DataCarNews);
                $this->assign('DataCarOlds',$DataCarOlds);
                $this->assign('Type',"chexin");
                break;
            case  "jinxiaoshang" :

                //经销商
                $StrSqlJXS = "select GClassID,company,domain,address,saletel,ismember from group_sort where classidt LIKE '%".$DataCar[0]["ParentID"].",%' and area_name='".C("APP_URL_AREA")."'";
                $DataJXS =  $Model->query( $StrSqlJXS );
                $this->assign('DataJXS',$DataJXS);

                $this->assign('Type',"jinxiaoshang");
                break;
            case  "tupian":

                $StrSqlCarP = "select id, title, PicPath,ClassID,GType from (select *,row_number() over(partition by gtype order by gtype) as rn from group_pic WHERE ClassID = '".$ClassID."') as a where rn<=3 ";
                $DataCarP = $Model->query( $StrSqlCarP );

                $this->assign('DataCarP',$DataCarP);
                $this->assign('Type',"tupian");
                break;
            case  "xinwen":
                //文章
                $DataArt =  $this->searchArt( " and (a.Title Like '%".$DataCar[0]["ClassName"]."%' Or a.Keyword Like '%".$DataCar[0]["ClassName"]."%')" ,10,0);

                $this->assign('DataArt',$DataArt);
                $this->assign('Type',"xinwen");
                break;
            case  "zongshu" :
                //车型
                $StrSqlCars = "select top 5 gc.id,gc.title,gc.classid,gc.pz,gc.csprice,gc.thumbnail from Group_Car gc where gc.classid='".$ClassID."' and not_new=".$notNew."  Order By gc.nf desc";
                $DataCars= $Model->query( $StrSqlCars );

                //经销商
                $StrSqlJXS = "select top 4 GClassID,company,domain,address,saletel,ismember from group_sort where classidt LIKE '%".$DataCar[0]["ParentID"].",%' and area_name='".C("APP_URL_AREA")."'";
                $DataJXS =  $Model->query( $StrSqlJXS );

                //文章
                $StrSqlArt = "select Top 10 a.ID,a.Title,a.ClassID,a.LinkUrl,u.classname from Uni_Article a Left join Uni_Class u on a.classid=u.classid Where (a.Title Like '%".$DataCar[0]["ClassName"]."%' Or a.Keyword Like '%".$DataCar[0]["ClassName"]."%') and a.area_name='".C("APP_URL_AREA")."' Order By IsTop Desc,ID Desc";
                $DataArt =  $Model->query( $StrSqlArt );

                //竞争车型
                $StrSqlCCar = "select top 4 gc.id, gc.ClassID,gc.ClassName,gcr.jibie,gcr.thumbnail,gcr.mincsprice,gcr.maxcsprice,gcj.minjxsprice,gcj.maxjxsprice From Group_Class gc LEFT JOIN (SELECT ClassID,min(csprice) mincsprice,MAX(csprice) maxcsprice ,min(thumbnail) as thumbnail ,min(jibie) as jibie from Group_Car GROUP BY ClassID) gcr  ON gcr.ClassID = gc.ClassID LEFT JOIN (SELECT ClassID,min(jxsPrice) minjxsprice,MAX(jxsPrice) maxjxsprice from Group_Car_Jxs GROUP BY classid ) gcj on gcj.ClassID = gc.ClassID where gcr.thumbnail is not null and gc.ClassID <> '".$DataCar[0]["ClassID"]."' ORDER BY  abs(gcr.mincsprice+gcr.maxcsprice - ".$DataCar[0]["mincsprice"]." -".$DataCar[0]["maxcsprice"].") asc";
                $DataCCar =  $Model->query( $StrSqlCCar );


                $this->assign("DataCCar",$DataCCar);
                $this->assign('DataArt',$DataArt);
                $this->assign('DataJXS',$DataJXS);
                $this->assign('DataCars',$DataCars);
                $this->assign('Type',"zongshu");
                break;
        }

        $this->assign('DataCar',$DataCar);
    }

    /**
     * JSON返回
     * @param $ClassID
     * @param int $pageSize
     * @param int $page
     */
    public function seaCarArt($carName,$pageSize = 10,$page = 0){

        $where  = " and (a.Title Like '%".$carName."%' Or a.Keyword Like '%".$carName."%')";
        $DataUc = $this -> searchArt($where,$pageSize,$page);
        if ($DataUc){
            $this->ajaxReturn($DataUc,"查询成功！",1,'JSON');
        }else{
            $this->ajaxReturn(0,"没有数据了！",0,'JSON');
        }
    }

    /**
     * 浏览图片详情
     * @param $imgClassID
     */
    public function carImg($carId = "",$carPhotoType = "",$classId = ""){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表

        if ($carPhotoType == ""){
            //详细图片
            $StrSqlPhotoClass = "select gp.id, gp.title, gp.PicPath,gp.BigPic,gp.ClassID,gp.GType,gc.ClassName from group_pic gp ,Group_Class gc where gc.ClassID = gp.ClassID and gp.id = '".$carId."'";
            $DataPhotoClass= $Model->query( $StrSqlPhotoClass );
        }else{
            //分类查询
            $StrSqlPhotoClass = "select top 1 gp.id, gp.title, gp.PicPath,gp.BigPic,gp.ClassID,gp.GType,gc.ClassName from group_pic gp ,Group_Class gc where gc.ClassID = gp.ClassID and gp.Gtype = '".$carPhotoType."' and gp.classid = '".$classId."' order by gp.id asc ";
            $DataPhotoClass= $Model->query( $StrSqlPhotoClass );
        }

        if($DataPhotoClass){
            //所有当前分类图片
            $StrSqlPhoto ="select id, title, PicPath,BigPic,ClassID,GType ,ROW_NUMBER() OVER (ORDER BY id asc) AS RowNumber  from group_pic where ClassID = '".$DataPhotoClass[0]['ClassID']."' and gtype = '".$DataPhotoClass[0]['GType']."' ORDER BY id asc ";
            $DataPhoto= $Model->query( $StrSqlPhoto );

            //当前是第几张
            $StrSqlIndexP = "select t.RowNumber FROM (select top 800 id, title, PicPath,BigPic,ClassID,GType ,ROW_NUMBER() OVER (ORDER BY id asc) AS RowNumber  from group_pic where ClassID = '".$DataPhotoClass[0]['ClassID']."' and gtype = '".$DataPhotoClass[0]['GType']."' ORDER BY id asc ) t where t.id = '".$DataPhotoClass[0]['id']."'";
            $DataIndwxP= $Model->query( $StrSqlIndexP );

            //图片类型
            $StrSqlPType = "SELECT count(GTYPE) AS Count, ClassID , GType from group_pic WHERE ClassID = '".$DataPhotoClass[0]['ClassID']."' GROUP BY GType,ClassID";
            $DataPType= $Model->query( $StrSqlPType );


            $this->assign('DataIndwxP',$DataIndwxP);
            $this->assign('DataPType',$DataPType);
            $this->assign('DataPhotoClass',$DataPhotoClass);
            $this->assign('DataPhoto',$DataPhoto);

            $this->display();
        }else{
            $this->error('没有找到该图片集！');
        }
    }

    /**
     * 分页返回车型相关文章列表
     * @param $Where
     * @param int $pageSize
     * @param int $page
     * @return array|mixed
     */
    public  function  searchArt($Where,$pageSize = 10,$page = 0){
        $Model = new Model();
        $StrSqlUc = "SELECT * from (select top ".intval($pageSize)*(intval($page)+1)." a.ID,a.Title,a.Keywords,CONVERT(varchar(100), a.DateAndTime, 120) as DateAndTime,a.IsTop,a.copyfrom,a.author,a.hits,a.Images,a.Images1,a.LinkUrl from Uni_Article a Left join Uni_Class u on a.classid=u.classid Where 1=1  ".$Where." and a.area_name='".C("APP_URL_AREA")."'  order by a.IsTop desc,a.ID desc) as art where  art.id not in (select top ".intval($pageSize)*intval($page)." a.ID from Uni_Article a Left join Uni_Class u on a.classid=u.classid Where 1=1 ".$Where." and a.area_name='".C("APP_URL_AREA")."'  order by a.IsTop desc,a.ID desc)";
        $DataUc = $Model->query( $StrSqlUc );

        return $DataUc;
    }

    /**
     * ajax搜索车型
     * @param string $parentID
     * @param string $price
     * @param string $stype
     * @param int $pageSize
     * @param int $page
     */
    public function searchType($parentID = "",$price = 99,$stype = 99,$pageSize = 10,$page = 0){
        $DataGCcar = $this -> searchCar($parentID ,$price ,$stype ,$pageSize ,$page );

        if ($DataGCcar){
            // 成功后返回客户端新增的用户ID，并返回提示信息和操作状态
            $this->ajaxReturn($DataGCcar,"查询成功！",1);
        }else{
            // 错误后返回错误的操作状态和提示信息
            $this->ajaxReturn(0,"查询错误！",0);
        }

    }

    /**
     * 关键字搜索车型
     * @param $keyword
     */
    public function search($keyword){
        if($keyword != ""){
            $Model = new Model();
            $StrSqlGCSearch = "select ClassID,ClassName,Depth from Group_Class where  ClassName LIKE '%".$keyword."%' and Depth <> '2' ORDER BY Depth ASC";
            $DataGCSearch = $Model->query( $StrSqlGCSearch );
            if(count($DataGCSearch) == 0){
                $this->assign('DataCount',count($DataGCSearch));
                $this->error('没有该车型！');
            }elseif(count($DataGCSearch) ==1){
                $this->Carcar($DataGCSearch[0]['ClassID'],"zongshu");
                $this->display('car');
            }else{
                if($DataGCSearch[0]['Depth'] == 1){
                    $this->CarLibIndex($DataGCSearch[0]['ClassID'],$price = 99,$stype = 99,$pageSize = 10,$page = 0,$order = "up");
                    $this->display('index');
                }else{

                    foreach ($DataGCSearch as $k=>&$n){
                        if($k == 0){
                            $StrClassIds = "'" .$DataGCSearch[$k]["ClassID"]."'";
                        }else{
                            $StrClassIds = $StrClassIds.",'" .$DataGCSearch[$k]["ClassID"]."'";
                        }
                    }

                    $StrSqlGcCar = "select gc.id, gc.ClassID,gc.ClassName,gcr.jibie,gcr.thumbnail,gcr.mincsprice,gcr.maxcsprice,gcj.minjxsprice,gcj.maxjxsprice From Group_Class gc LEFT JOIN (SELECT ClassID,min(csprice) mincsprice,MAX(csprice) maxcsprice ,min(thumbnail) as thumbnail ,min(jibie) as jibie from Group_Car GROUP BY ClassID) gcr  ON gcr.ClassID = gc.ClassID LEFT JOIN (SELECT ClassID,min(jxsPrice) minjxsprice,MAX(jxsPrice) maxjxsprice from Group_Car_Jxs GROUP BY classid ) gcj on gcj.ClassID = gc.ClassID where gcr.thumbnail is not null  and gc.ClassID in(".$StrClassIds.")  ORDER BY  gcr.mincsprice DESC";
                    $DataGCcar = $Model->query( $StrSqlGcCar );

                    $this->assign('DataGCcar',$DataGCcar);
                    $this->assign('keyword',$keyword);
                    $this->display();
                }
            }
        }else{
            $this->error('关键字错误！');
        }

    }

    /**
     * 车型库
     * @param string $parentID
     * @param int $price
     * @param int $stype
     * @param int $pageSize
     * @param int $page
     * @param string $order
     */
    public function  CarLibIndex($parentID = "",$price = 99,$stype = 99,$pageSize = 10,$page = 0,$order = "up"){
        $Model = new Model(); // 实例化一个model对象 没有对应任何数据表
        $StrSqlGC = "Select ClassID,ClassName,Zimu From Group_Class Where ParentID='0' Order By zimu Asc,Orders Asc";
        $DataGc = $Model->query( $StrSqlGC );

        $DataGCcar = $this -> searchCar($parentID,$price,$stype ,$pageSize,$page,$order);


        if($parentID != ""){
            $StrSqlGcSel = "SELECT ClassName FROM Group_Class WHERE classid = '".$parentID."'";
            $DataGcSel = $Model->query( $StrSqlGcSel );
            $ClassName = $DataGcSel[0]['ClassName'];
        }else{
            $ClassName = "";
        }

        $this->assign('stype',$stype);
        $this->assign('price',$price);
        $this->assign("className",$ClassName);
        $this->assign('parentID',$parentID);
        $this->assign('DataGc',$DataGc);
        $this->assign('DataGCcar',$DataGCcar);
    }

    /**
     * 查询车型
     * @param string $parentID
     * @param string $price
     * @param string $stype
     * @param int $pageSize
     * @param int $page
     * @return array|mixed
     */
    public function searchCar($parentID = "",$price = 99,$stype = 99,$pageSize = 10,$page = 0,$order = "up"){
        $Model = new Model();
        $where = "";
        $orders = "";
        if($order == "up"){
            $orders = " gcr.mincsprice ASC ";
        }else{
            $orders = " gcr.mincsprice DESC ";
        }

        if($parentID != ""){
            $StrSqlGcSub = "SELECT ClassID FROM Group_Class WHERE ParentID = '".$parentID."'";
            $DataGcSub = $Model->query( $StrSqlGcSub );
            $StrClassIds = "";
            foreach ($DataGcSub as $k=>&$n){
                if($k == 0){
                    $StrClassIds = "'" .$DataGcSub[$k]["ClassID"]."'";
                }else{
                    $StrClassIds = $StrClassIds.",'" .$DataGcSub[$k]["ClassID"]."'";
                }
            }

            $where = " and gc.ParentID in (".$StrClassIds.")";
        }

        if($price != 99 ){
            $priceArr = C(CAR_PRICE);
            $price = $priceArr[$price]['VALUE'];
            $priceArr =  preg_split ( "/-/" ,  $price);
            $where  = $where. " and gcr.mincsprice > ".$priceArr[0]." and gcr.maxcsprice < ".$priceArr[1]."";
        }

        if($stype != 99){
            $stypeArr = C(CAR_TYPE);
            $stype = $stypeArr[$stype]['VALUE'];
            $where = $where. " and gcr.jibie LIKE '%".$stype."%' ";
        }

        $StrSqlGCcar = "SELECT * from (select top ".intval($pageSize)*(intval($page)+1)." gc.id, gc.ClassID,gc.ClassName,gcr.jibie,gcr.thumbnail,gcr.mincsprice,gcr.maxcsprice,gcj.minjxsprice,gcj.maxjxsprice From Group_Class gc LEFT JOIN (SELECT ClassID,min(csprice) mincsprice,MAX(csprice) maxcsprice ,min(thumbnail) as thumbnail ,min(jibie) as jibie from Group_Car GROUP BY ClassID) gcr  ON gcr.ClassID = gc.ClassID LEFT JOIN (SELECT ClassID,min(jxsPrice) minjxsprice,MAX(jxsPrice) maxjxsprice from Group_Car_Jxs GROUP BY classid ) gcj on gcj.ClassID = gc.ClassID where gcr.thumbnail is not null ".$where." ORDER BY  ".$orders.") as gcl where  gcl.id not in (select top ".intval($pageSize)*intval($page)." gc.id From Group_Class gc LEFT JOIN (SELECT ClassID,min(csprice) mincsprice,MAX(csprice) maxcsprice ,min(thumbnail) as thumbnail ,min(jibie) as jibie from Group_Car GROUP BY ClassID) gcr  ON gcr.ClassID = gc.ClassID LEFT JOIN (SELECT ClassID,min(jxsPrice) minjxsprice,MAX(jxsPrice) maxjxsprice from Group_Car_Jxs GROUP BY classid ) gcj on gcj.ClassID = gc.ClassID where gcr.thumbnail is not null ".$where." ORDER BY  ".$orders.")";
        $DataGCcar = $Model->query( $StrSqlGCcar );

        return $DataGCcar;
    }

}